package com.hackordie.commons.software.applications;

import java.util.Random;

public class PasswordDecodifier extends AApplication {

    String codifierPassword;
    
    public PasswordDecodifier(String name, String description, int size,
            int requiredRam, int requiredProcessor) {
        super(name, description, size, requiredRam, requiredProcessor);
        
        codifierPassword = "";
    }

    @Override
    public boolean run(Object[] obj) {
        
        String codifierPassword = (String) obj[0];
        int decodifierLevel = (Integer) obj[1];
        
        boolean minus = false;
        boolean mayus = false;
        boolean numbers = false;
        boolean special = false;

        if (codifierPassword.matches("^.*\\d.*$")) {
            numbers = true;
        }

        if (codifierPassword.matches("^[A-Z]*$")) {
            mayus = true;
        }

        if (codifierPassword.matches("^[a-z]*$")) {
            minus = true;
        }

        if (codifierPassword.matches("^[-|_|*|^|<|>]*$")) { // TODO buscar
                                                            // expresion regular
                                                            // para
                                                            // caracteres
                                                            // especiales
            special = true;
        }

        // dificultad base: 10%
        int dificultAccess = 10; // TODO pasar a constante

        // +20% por cada condicion, un maximo de un 90% de dificultad
        if (numbers) {
            dificultAccess += 20;
        }
        if (mayus) {
            dificultAccess += 20;
        }
        if (minus) {
            dificultAccess += 20;
        }
        if (special) {
            dificultAccess += 20;
        }

        // dificultad baja un 10% por nivel de skill //TODO ajustar, lvls de 0 a
        // 100
        dificultAccess -= (decodifierLevel);

        Random rnd = new Random();

        if (rnd.nextInt(100) > dificultAccess) {
            this.codifierPassword = codifierPassword;
            return true;
        }
        
        return false;
    }

    @Override
    public boolean checkObj(Object[] obj) {
        if( !(obj.length != 3              &&
                obj[1] instanceof String && 
                obj[2] instanceof Integer)){
             
            return false;
          }
        return true;
    }

}
